﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace BackLib
{
    public class ControlStatus
    {
        Dictionary<string, string[]> keys = new Dictionary<string, string[]>();
        Label statusLabel;
        List<string> controlNameList = new List<string>();
        Timer resetTimer = new Timer();

        List<Color> colors = new List<Color>();
        int currentColorIndex = 0;
        int tickIndex = 0;

        public ControlStatus(Label statusLabel)
        {
            CreateColorBlend();

            string rawLines = global::BackLib.Properties.Resources.tips;
            string[] rawSplitFirst = SplitByString(rawLines, "\r\n*");
            
            foreach(string entry in rawSplitFirst)
            {
                string[] rawSplitSecond = SplitByString(entry, "\r\n-");
                List<string> subs = new List<string>(rawSplitSecond);
                subs.RemoveAt(0);
                keys.Add(rawSplitSecond[0], subs.ToArray());
                controlNameList.Add(rawSplitSecond[0]);
            }

            resetTimer.Interval = 70;
            resetTimer.Tick += new EventHandler(resetTimer_Tick);

            this.statusLabel = statusLabel;
        }

        void resetTimer_Tick(object sender, EventArgs e)
        {
            if (tickIndex < 30)
            {
                tickIndex++;
            }
            else
            {
                if (currentColorIndex < 4)
                {
                    currentColorIndex++;
                    statusLabel.ForeColor = colors[currentColorIndex];
                }
                else
                {
                    ResetVars();
                }
            }
        }

        void ResetVars()
        {
            currentColorIndex = 0;
            tickIndex = 0;
            statusLabel.ForeColor = colors[0];
            resetTimer.Stop();
            statusLabel.Text = "";
        }

        public void DisplayTip(Control control)
        {
            ResetVars();
            resetTimer.Start();
            statusLabel.Text = GetString(control);
        }

        public void CreateEvents(Control[] parents)
        {
            foreach (Control parent in parents)
            {
                foreach (string name in controlNameList)
                {
                    try
                    {
                        parent.Controls.Find(name, true)[0].MouseHover += new EventHandler(MouseOverTip);
                    }
                    catch { }
                }
            }
        }

        void MouseOverTip(object sender, EventArgs e)
        {
            DisplayTip((Control)sender);
        }

        private string[] SplitByString(string testString, string split)
        {
            int offset = 0;
            int index = 0;
            int[] offsets = new int[testString.Length + 1];

            while (index < testString.Length)
            {
                int indexOf = testString.IndexOf(split, index);
                if (indexOf != -1)
                {
                    offsets[offset++] = indexOf;
                    index = (indexOf + split.Length);
                }
                else
                {
                    index = testString.Length;
                }
            }

            string[] final = new string[offset + 1];
            if (offset == 0)
            {
                final[0] = testString;
            }
            else
            {
                offset--;
                final[0] = testString.Substring(0, offsets[0]);
                for (int i = 0; i < offset; i++)
                {
                    final[i + 1] = testString.Substring(offsets[i] + split.Length, offsets[i + 1] - offsets[i] - split.Length);
                }
                final[offset + 1] = testString.Substring(offsets[offset] + split.Length);
            }
            return final;
        }

        private string GetString(Control control)
        {
            return keys[control.Name][int.Parse(control.Tag.ToString())];
        }

        private void CreateColorBlend()
        {
            string[] hexArrays = new string[] { "c6d5e2",  "678aa8", "3a5c7a", "1a364f", "061420" };

            foreach (string hex in hexArrays)
            {
                colors.Add(ColorTranslator.FromHtml("#" + hex));
            }
        }
    }
}
